package com.tinem.platform.service.data.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.tinem.platform.module.pojo.vo.data.LanguageRes;
import com.tinem.platform.module.pojo.vo.data.TranslateInfo;
import com.tinem.platform.module.pojo.vo.data.TranslateVO;
import com.tinem.platform.module.starter.jpa.entity.translate.TranslateLanguage;
import com.tinem.platform.module.starter.jpa.repository.translate.TranslateLanguageRepository;
import com.tinem.platform.service.data.service.TranslateService;
import com.tinem.platform.service.data.utils.TransUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: platform
 * @description: ${description}
 * @author: eden#can
 * @Slogan: 没有所谓失败，除非你不再尝试
 * @date: 2022-01-08 13:16
 **/
@Slf4j
@Service
public class TranslateServiceImpl implements TranslateService {

    @Resource
    TransUtil transUtil;

    @Override
    public List<LanguageRes> languageList(String lang) {

        ArrayList<LanguageRes> list = new ArrayList<>();
//        list.add(new LanguageRes("zh_cn", "中文"));
//        list.add(new LanguageRes("zh_tw", "繁體中文"));
//        list.add(new LanguageRes("en_us", "English"));
//        list.add(new LanguageRes("ja_jp", "日本語"));
//        list.add(new LanguageRes("ko_kr", "한국어"));
//        list.add(new LanguageRes("fr_fr", "Le français"));
//        list.add(new LanguageRes("de_de", "Deutsch"));
//        list.add(new LanguageRes("it_it", "lingua italiana"));
//        list.add(new LanguageRes("es_es", "Español"));
//        list.add(new LanguageRes("th_th", "ไทย"));

        // 中文 Chinese 繁体中文 Traditional Chinese   英语 English 日语 Japanese 韩语 Korean 法语
        // French 德语 German 意大利语 Italian 西班牙语 Spanish  泰语 Thai
        if(StrUtil.equals(lang,"zh_cn")){
            list.add(new LanguageRes("zh_cn", "中文"));
            list.add(new LanguageRes("zh_tw", "繁体中文"));
            list.add(new LanguageRes("en_us", "英语"));
            list.add(new LanguageRes("ja_jp", "日语"));
            list.add(new LanguageRes("ko_kr", "韩语"));
            list.add(new LanguageRes("fr_fr", "法语"));
            list.add(new LanguageRes("de_de", "德语"));
            list.add(new LanguageRes("it_it", "意大利语"));
            list.add(new LanguageRes("es_es", "西班牙语"));
            list.add(new LanguageRes("th_th", "泰语"));
        } else {
            list.add(new LanguageRes("zh_cn", "Chinese"));
            list.add(new LanguageRes("zh_tw", "Traditional Chinese"));
            list.add(new LanguageRes("en_us", "English"));
            list.add(new LanguageRes("ja_jp", "Japanese"));
            list.add(new LanguageRes("ko_kr", "Korean"));
            list.add(new LanguageRes("fr_fr", "French"));
            list.add(new LanguageRes("de_de", "German"));
            list.add(new LanguageRes("it_it", "Italian"));
            list.add(new LanguageRes("es_es", "Spanish"));
            list.add(new LanguageRes("th_th", "Thai"));
        }

        return list;
    }


    @Resource
    TranslateLanguageRepository translateLanguageRepository;


    @Override
    public String getTranslateResult(String sha256) {
        log.info("api get chache translate, msg_sha256:{}",sha256);
        Optional<TranslateLanguage> msgLanOptional = translateLanguageRepository.findById(sha256);
        if (!msgLanOptional.isPresent()) {
            return null;
        }
        TranslateLanguage msgLan = msgLanOptional.get();
        String tarMsg = msgLan.getTarMsg();
        log.info("api get chache translate, resStr:{}",tarMsg);
        return tarMsg;
    }

    @Override
    public List<TranslateInfo> getTranslateByBaiduResult(List<String> list, TranslateVO req) {

        String queryText = buildQueryText(list);

        JSONArray transResult = transUtil.getTransResult(queryText, req.getFromLan(), req.getToLan());
        List<TranslateInfo> results = null;
        if(transResult != null){
            results = transResult.toList(TranslateInfo.class);
            // 保存翻译数据
            saveTranslateResult(results,req);
        }
        return results;
    }

    private String buildQueryText(List<String> list) {

        return StringUtils.join(list.toArray(),"\n");
    }

    /**
     * 保存翻译结果到数据库
     * @param translateRes
     */
    public void saveTranslateResult(List<TranslateInfo> translateRes,TranslateVO req) {

        List<TranslateLanguage> languages = new ArrayList<>();

        translateRes.stream().forEach(translateInfo -> {
            String shaStr = StrUtil.format("{}{}{}", translateInfo.getSrc().trim(), req.getFromLan(), req.getToLan());
            String sha256 = SecureUtil.sha256(shaStr);
            // 增加判断，数据库中是否存在该条翻译数据，若存在则不存数据库
            Optional<TranslateLanguage> msgLanOptional = translateLanguageRepository.findById(sha256);
            if (msgLanOptional.isPresent()) {
                return ;
            }
            TranslateLanguage translateLanguage = new TranslateLanguage();
            translateLanguage.setId(sha256);
            translateLanguage.setMsg(translateInfo.getSrc());
            translateLanguage.setTarMsg(translateInfo.getDst());
            translateLanguage.setMsgLan(req.getFromLan());
            translateLanguage.setTarLan(req.getToLan());
            translateLanguage.setUpdatedTime(new Date());
            translateLanguage.setCreatedTime(new Date());
            languages.add(translateLanguage);
        });
        log.info("saveTranslateResult,{}",JSONUtil.toJsonStr(languages));
        translateLanguageRepository.saveAll(languages);
    }


    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        ArrayList<String> list1 = new ArrayList<>();
        list.add("你");
        list.add("我");
        list.add("他");

        list1.add(null);

        list.addAll(list1);


        String join = StringUtils.join(list.toArray(), "\n");

        System.out.println(join);
    }

}
